package other.xsy.passlys;

import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEvent;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventRequest;
import com.rkhd.platform.sdk.creekflow.autoflowevent.AutoFlowEventResponse;
import com.rkhd.platform.sdk.data.model.*;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.OperateResult;
import org.apache.commons.lang.StringUtils;
import other.xsy.passlys.util.HCXObjectService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: Booking自动创建，添加销售为团队成员
 * @Author: 黎炎松
 * @Date: 2024/4/19 16:48
 */
public class BookingAddTeamAutoFlow implements AutoFlowEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "Booking自动创建，添加销售为团队成员 ==> ";

    @Override
    public AutoFlowEventResponse execute(AutoFlowEventRequest request) throws ScriptBusinessException {
        logger.info(LOGGER_MSG + " 触发开始 ");
        logger.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId() + " , ProcessInstanceId:" + request.getProcessInstanceId());
        logger.info(request.toString());
        AutoFlowEventResponse response = new AutoFlowEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        addTeamByBooking(request.getDataId());
        return response;
    }

    public static void main(String[] args) throws ApiEntityServiceException {
        BookingAddTeamAutoFlow bookingAddTeamAutoFlow = new BookingAddTeamAutoFlow();
        //bookingAddTeamAutoFlow.addTeamByBooking(3256364351376688L);
    }


    /**
     *  根据Booking自动创建，添加销售为团队成员
     * @param bookingId
     */
    private void addTeamByBooking(Long bookingId) {
        try {
            logger.debug(LOGGER_MSG + " 处理开始: " + bookingId);

            // 获取booking数据
            Booking__c booking = new Booking__c();
            booking.setId(bookingId);
            booking = hcxObjectService.get(booking);
            logger.debug(LOGGER_MSG + " booking: " + booking);

            // 添加团队成员
            TeamMember teamMember = new TeamMember();
            teamMember.setEntityType(11010064400001L);
            teamMember.setOwnerFlag(2); // 1: 有修改权限 2: 只可以查看
            teamMember.setUserId(booking.getBookingSales__c());
            teamMember.setRecordFrom_data(booking.getId());
            teamMember.setRecordFrom(3226444676959521L);
            OperateResult insert = hcxObjectService.insert(teamMember);
            if (!insert.getSuccess()) {
                logger.debug(LOGGER_MSG + " 添加团队成员失败: " + insert.getErrorMessage());
            }

        } catch (ApiEntityServiceException e) {
            logger.debug(LOGGER_MSG + " 处理异常");
        }
    }

}
